package ar.com.android.restoapp.providers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import ar.com.android.restoapp.dbHelpers.SQLFoodOrderHelper;
import ar.com.android.restoapp.entities.Order;
import ar.com.android.restoapp.utils.Constants;

public class OrderProvider {
	private static final String TAG = "OrderProvider";
	
	private static final String DB_COLUMN_0_NAME = "idOrderFood";
	private static final String DB_COLUMN_1_NAME = "idTable";
	private static final String DB_COLUMN_2_NAME = "numGuests";
	private static final String DB_COLUMN_3_NAME = "state";
	private static final String DB_COLUMN_4_NAME = "dateFrom";
	private static final String DB_COLUMN_5_NAME = "dateTo";
	
	private SQLiteDatabase database;
	private SQLFoodOrderHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME, DB_COLUMN_2_NAME, DB_COLUMN_3_NAME, DB_COLUMN_4_NAME, DB_COLUMN_5_NAME};
	
	public OrderProvider(Context context) {
		dbHelper = SQLFoodOrderHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
		
	public long insertOrder(Order order)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);
		
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, order.getIdTable());
		contentValues.put(DB_COLUMN_2_NAME, order.getNumGuest());
		contentValues.put(DB_COLUMN_3_NAME, order.getState());
		contentValues.put(DB_COLUMN_4_NAME, order.getDateFrom() != null ? sdf.format(order.getDateFrom()) : "");
		contentValues.put(DB_COLUMN_5_NAME, order.getDateTo() != null ? sdf.format(order.getDateTo()) : "");
		
		long insertId = database.insert(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, null, contentValues);
		return insertId;
	}
	
	public boolean removeOrder(Integer idOrder)
	{
		int result = database.delete(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idOrder, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeOrder()
	{
		int result = database.delete(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, null, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeOrderTable(Integer idTable)
	{
		int result = database.delete(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, DB_COLUMN_1_NAME + " = " + idTable, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public long updateOrder(Order order)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);
		
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, order.getIdTable());
		contentValues.put(DB_COLUMN_2_NAME, order.getNumGuest());
		contentValues.put(DB_COLUMN_3_NAME, order.getState());
		contentValues.put(DB_COLUMN_4_NAME, order.getDateFrom() != null ? sdf.format(order.getDateFrom()) : "");
		contentValues.put(DB_COLUMN_5_NAME, order.getDateTo() != null ? sdf.format(order.getDateTo()) : "");
		
		return database.update(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + order.getIdOrder(), null);
	}
	
	public List<Order> getAllOrder() {
		
		Cursor cursor = null;
		List<Order> orders = new ArrayList<Order>();
		
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);
			
			cursor = database.query(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, allColumns, null, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
				Order order = new Order();
				order.setIdOrder(cursor.getInt(0));
				order.setIdTable(cursor.getInt(1));
				order.setNumGuest(cursor.getInt(2));
				order.setState(cursor.getString(3));
				order.setDateFrom(sdf.parse(cursor.getString(4)));
				order.setDateTo(sdf.parse(cursor.getString(5)));
				
				orders.add(order);
				cursor.moveToNext();
			}
			
			return orders;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return orders;
		}
		finally {
			cursor.close();
		}
	}

	public Order getOrder(Integer idOrder) 
	{
		Cursor cursor = null;
		Order order = null;
				
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);
			
			cursor = database.query(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, allColumns, DB_COLUMN_0_NAME + " = " + idOrder, null, null, null, null);
			cursor.moveToFirst();
			
			order = new Order();
			order.setIdOrder(cursor.getInt(0));
			order.setIdTable(cursor.getInt(1));
			order.setNumGuest(cursor.getInt(2));
			order.setState(cursor.getString(3));
			order.setDateFrom(sdf.parse(cursor.getString(4)));
			order.setDateTo(sdf.parse(cursor.getString(5)));
			
			return order;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return order;
		}
		finally {
			cursor.close();
		}
	}
	
	public Order getOrder(Integer idTable, String state)
	{
		Cursor cursor = null;
		Order order = null;
		
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);
			
			cursor = database.query(SQLFoodOrderHelper.DB_ORDER_TABLE_NAME, allColumns, DB_COLUMN_3_NAME + " = '" + state + "' AND " + DB_COLUMN_1_NAME + " = " + idTable, null, null, null, null);
			cursor.moveToFirst();
			
			order = new Order();
			order.setIdOrder(cursor.getInt(0));
			order.setIdTable(cursor.getInt(1));
			order.setNumGuest(cursor.getInt(2));
			order.setState(cursor.getString(3));
			order.setDateFrom(sdf.parse(cursor.getString(4)));
			order.setDateTo(sdf.parse(cursor.getString(5)));
			
			cursor.close();
			return order;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return order;
		}
		finally {
			cursor.close();
		}
	}
}
